//
//  XPNavigationController.swift
//  PPKit
//
//  Created by xiaopin on 16/8/31.
//  Copyright © 2016年 pinguo. All rights reserved.
//

import UIKit

class XPNavigationController: UINavigationController {

    override var shouldAutorotate : Bool {
        
//        if self.topViewController?.isKind(of: MTMatchDetailViewController.self) ?? false
//            {
//            return true
//        }
        
        return false
    }
    
    override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
//        if self.topViewController?.isKind(of: MTMatchDetailViewController.self) ?? false
//            {
//            return .allButUpsideDown
//        }
        
        return .portrait
    }
    
    override var preferredInterfaceOrientationForPresentation : UIInterfaceOrientation {
        return .portrait
    }
    
    /// 重写此方法让 preferredStatusBarStyle 响应
    override var childForStatusBarStyle: UIViewController? {
        return self.topViewController
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //1.解决右滑返回失效问题
        self.interactivePopGestureRecognizer?.delegate = self
        
        let backColor = pp_color(.CFFFFFF)
        let foreColor = pp_color(.C222222)
        let font = UIFont.boldSystemFont(ofSize: 17)
        if #available(iOS 13.0, *){
            let appearance = UINavigationBarAppearance.init()
            appearance.configureWithOpaqueBackground()
            //设置字体颜色、字体大小、背景颜色、背景图片
            appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor:foreColor,
                                              NSAttributedString.Key.font:font]
            appearance.backgroundColor = backColor
            appearance.backgroundEffect = nil
            //appearance.backgroundImage = UIImage()
            //去除导航栏阴影（如果不设置clear，导航栏底下会有一条阴影线）
            //appearance.shadowColor = .clear
            navigationBar.scrollEdgeAppearance = appearance
            navigationBar.standardAppearance = appearance
            
            if #available(iOS 15.0, *) {
                navigationBar.compactAppearance = appearance
                navigationBar.compactScrollEdgeAppearance = appearance
            }
        }else{
            navigationBar.isTranslucent = false
            navigationBar.barTintColor = backColor
            navigationBar.tintColor = foreColor
            navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor:foreColor,
                                                 NSAttributedString.Key.font:font]
            navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        }
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        /*
         然后你可以使用下面这句代码来控制是否允许右滑返回，这句代码要加在 viewDidAppear 里面，否则多个页面切换时会出现异常。
         */
        // 2.是否允许右滑返回
        //self.interactivePopGestureRecognizer?.isEnabled = true;
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        
        if #available(iOS 13.0, *){
            //外观模式变更
            if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection){
                //如果系统外观模式切换了，更新适配
                
            }
        }
    }
}

extension XPNavigationController:UIGestureRecognizerDelegate{
    ///3.这个方法是在手势将要激活前调用：返回YES允许右滑手势的激活，返回NO不允许右滑手势的激活
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer == self.interactivePopGestureRecognizer {
            //屏蔽调用rootViewController的滑动返回手势，避免右滑返回手势引起死机问题
            if self.viewControllers.count < 2 || self.visibleViewController == self.viewControllers.first {
                return false
            }
        }
        return true
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        print(viewController)
        viewController.navigationItem.backBarButtonItem = UIBarButtonItem(title:"",style:.plain,target:nil,action:nil)
        if self.children.count == 1 {
            viewController.hidesBottomBarWhenPushed = true
        }
        super.pushViewController(viewController, animated:animated)
    }
}


/*
 导航栏各种右滑返回失效的解决方法：
 //https://www.jianshu.com/p/2b2be863bb79
 //https://juejin.im/post/5adeda3051882567336a5dc9
 */
